package com.weijianhuawen.wjblog.web.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleContentDo;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleDo;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.CategoryMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.StatisticsArticlePvMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.TagMapper;
import com.weijianhuawen.wjblog.common.utils.Response;
import com.weijianhuawen.wjblog.web.mode.vo.statistics.FindStatisticsInfoRspVo;
import com.weijianhuawen.wjblog.web.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-11-03 23:51
 */

@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {
    @Resource
    private StatisticsArticlePvMapper statisticsArticlePvMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private TagMapper tagMapper;
    @Override
    public Response findStatisticsInfo() {
        // 统计 文章总量 分类 标签 pv总量
        Long articleTotalCount = articleMapper.selectCount(Wrappers.emptyWrapper());
        Long categoryTotalCount = categoryMapper.selectCount(Wrappers.emptyWrapper());
        Long tagTotalCount = tagMapper.selectCount(Wrappers.emptyWrapper());
        Long pvTotalCount = 0L;

        // 获取文章阅读量
        List<ArticleDo> articleDoList = articleMapper.selectAllByReadNum();

        if (!CollectionUtils.isEmpty(articleDoList)) {
            pvTotalCount = articleDoList.stream().mapToLong(ArticleDo::getReadNum).sum();
        }

        // 封装为vo
        FindStatisticsInfoRspVo vo = FindStatisticsInfoRspVo.builder()
                .articleTotalCount(articleTotalCount)
                .categoryTotalCount(categoryTotalCount)
                .tagTotalCount(tagTotalCount)
                .pvTotalCount(pvTotalCount)
                .build();

        return Response.success(vo);
    }
}
